import { add, get, list, treeList, menus, del } from '@/api/menu'

const menu = {
  state: {
    name
  },

  mutations: {
    SET_NAME: (state, { name }) => {
      state.name = name
    }
  },

  actions: {
    // 添加菜单
    MenuAdd ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        debugger
        add(userInfo).then(response => {
          const result = response.result
          // console.log('add menu result', result)
          commit('SET_TOKEN', result.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 删除菜单
    MenuDel ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        debugger
        del(userInfo).then(response => {
          const result = response.result
          console.log('add menu result', result)
          // commit('SET_TOKEN', result.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    MenuGet ({ commit }, menuForms) {
      return new Promise((resolve, reject) => {
        get(menuForms).then(response => {
          debugger
          const result = response.result
          // if (result.menus.length > 0) {
          //   console.log(result.menus)
          // } else {
          //   reject(new Error('getInfo: roles must be a non-null array !'))
          // }

          commit('SET_NAME', { id: result.id })

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
 // 获取菜单列表
    MenuList ({ commit }, menuForms) {
    return new Promise((resolve, reject) => {
      list(menuForms).then(response => {
        const result = response
        const results = response.results
        if (results.length > 0) {
          // console.log(results)
          // console.log(result.total)
        } else {
          reject(new Error('getInfo: roles must be a non-null array !'))
        }

        commit('SET_NAME', { name: result.pagination })

        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
    },
    // 获取菜单列表
    TreeList ({ commit }, menuForms) {
    return new Promise((resolve, reject) => {
      treeList(menuForms).then(response => {
        const result = response
        const results = response.results
        if (results.length > 0) {
          // console.log(results)
          // console.log(result.total)
        } else {
          reject(new Error('getInfo: roles must be a non-null array !'))
        }

        commit('SET_NAME', { name: result.pagination })

        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
    },
    // 获取菜单列表
    Menus ({ commit }, menuForms) {
    return new Promise((resolve, reject) => {
      menus(menuForms).then(response => {
        const result = response
        const results = response.results
        if (results.length > 0) {
          // console.log(results)
          // console.log(result.total)
        } else {
          reject(new Error('getInfo: roles must be a non-null array !'))
        }

        commit('SET_NAME', { name: result.pagination })

        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
    }

  }
}

export default menu
